package com.meitianhui.convenitenbuy.module.store.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.main.ui.MainActivity;
import com.meitianhui.convenitenbuy.module.home.ui.activity.web.FightGroupWebViewActivity;
import com.meitianhui.convenitenbuy.module.order.ui.activity.OrderActivity;
import com.meitianhui.convenitenbuy.module.store.adapter.GroupSaleAdapter;
import com.meitianhui.convenitenbuy.module.store.contract.GroupSaleContract;
import com.meitianhui.convenitenbuy.module.store.di.DaggerStoreComponent;
import com.meitianhui.convenitenbuy.module.store.di.StoreModule;
import com.meitianhui.convenitenbuy.module.store.model.GroupSaleInfo;
import com.meitianhui.convenitenbuy.module.store.presenter.GroupSalePresenter;
import com.meitianhui.convenitenbuy.util.JumpUtil;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/11/24.
 * 团购预售
 */

public class GroupSaleActivity extends BaseToolbarActivity implements GroupSaleContract.View{
    private String mStoreId;
    private RecyclerView mRvGroupSale;
    private View mNotDataView;
    private TextView mTvContent;
    private GroupSaleAdapter mAdapter;
    private List<GroupSaleInfo.ListBean> groupSaleInfoList;
    @Inject
    GroupSalePresenter presenter;

    @Override
    protected int setView() {
        return R.layout.activity_group_sale;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerStoreComponent
                .builder()
                .appComponent(App.getInstance().getAppComponent())
                .storeModule(new StoreModule(this))
                .build()
                .inject(this);
        mRvGroupSale = getViewById(R.id.rv_group_sale);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvGroupSale.setLayoutManager(gridLayoutManager);
        mNotDataView = getLayoutInflater().inflate(R.layout.data_empty_goods, (ViewGroup) mRvGroupSale.getParent(), false);
        mTvContent = (TextView) mNotDataView.findViewById(R.id.tv_content);
        mTvContent.setText("暂无团购预售");
        initData();
    }



    private void initData(){
        setTbTitle("团购预售");
        setTbMenu(R.mipmap.icon_store_detail_order);
        mStoreId = getIntent().getStringExtra("store_id");
        presenter.getGroupSale(mStoreId);
        mAdapter = new GroupSaleAdapter(null);
        mRvGroupSale.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(GroupSaleActivity.this, FightGroupWebViewActivity.class);
                intent.putExtra("url", Constants.HOST + "h5/c/presell/detail?invitation_code=" + groupSaleInfoList.get(position).getInvitation_code());
                intent.putExtra("title", "团购预售");
                intent.putExtra("type", "group-purchase");
                startActivity(intent);
            }
        });
    }

    /**
     * 点击右边的按钮
     */
    @Override
    protected void clickMenu() {
        super.clickMenu();
        if (!InfoProvider.getLoginStatus(this)) {
            JumpUtil.jumpToLoginActivity(0, GroupSaleActivity.this, 0, MainActivity.CURRENT_FRAGMENT_TAG_MINE);
            return;
        }
        Intent intent = new Intent(GroupSaleActivity.this, OrderActivity.class);
        intent.putExtra("order_type", 1);
        startActivity(intent);
    }

    @Override
    protected void onRefreshData() {
        presenter.getGroupSale(mStoreId);
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getGroupSaleSuccess(GroupSaleInfo data) {
        refreshFinish();
        final int size = data.getList() == null ? 0 : data.getList().size();
            mAdapter.setNewData(data.getList(),data.getDoc_url());
            if(size==0){
                mAdapter.setEmptyView(mNotDataView);
            }
        groupSaleInfoList = mAdapter.getData();
    }
}
